package com.dcjt.firehotcarpad.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.aries.library.fast.module.fragment.FastRefreshLoadFragment
import com.aries.library.fast.retrofit.FastObserver
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.adapter.AroundWaterAdapter
import com.dcjt.firehotcarpad.entity.AroundWaterEntity
import com.dcjt.firehotcarpad.entity.DisasterInfoEntity
import com.dcjt.firehotcarpad.retrofit1.repository.ApiRepository1
import com.trello.rxlifecycle2.android.ActivityEvent
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity


class AroundWaterFragment : FastRefreshLoadFragment<AroundWaterEntity.DataBeanXX.DataBeanX>() {
    private lateinit var mAdapter: AroundWaterAdapter
    private var disasterEntity: DisasterInfoEntity? = null

    companion object {
        fun newInstance(id: String): AroundWaterFragment {
            val fragment = AroundWaterFragment()
            val bundle = Bundle()
            bundle.putString("id", id)
            fragment.setArguments(bundle)
            return fragment
        }
    }


    override fun getLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(mContext)
    }

    override fun getContentLayout(): Int = R.layout.fast_layout_multi_status_refresh_recycler

    override fun initView(savedInstanceState: Bundle?) {
        mRecyclerView.addItemDecoration(
            DividerItemDecoration(
                requireContext(),
                DividerItemDecoration.VERTICAL
            )
        )
    }


    public fun setDisasterInfo(disasterEntity1 : DisasterInfoEntity) {
        disasterEntity = disasterEntity1
        loadData(0)
    }

    override fun isRefreshEnable(): Boolean {
        return true
    }

    override fun isLoadMoreEnable(): Boolean {
        return true
    }

    override fun loadData(page1: Int) {
        if (disasterEntity!= null)
        ApiRepository1.getInstance().getAroundWater(disasterEntity!!.id, page1)
            .compose((mContext as RxAppCompatActivity).bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe(object : FastObserver<List<AroundWaterEntity>>() {
                override fun _onNext(around: List<AroundWaterEntity>) {
                    mEasyStatusView.content()
                    var datu = around[0]
                    val entity = datu.data.data

                    if (entity == null || entity.isEmpty()) {
                        if (page1 == DEFAULT_PAGE && mAdapter.data.size == 0) { //第一页就么有
                            mEasyStatusView.empty()
                        } else {
                            mAdapter.loadMoreEnd()
                        }
                        return
                    }
                    mAdapter.loadMoreComplete()
                    if (mRefreshLayout.isRefreshing && page1 === 0) {
                        mAdapter.setNewData(ArrayList())
                        mRefreshLayout.finishRefresh()
                    }
                    mAdapter.addData(entity)

                    if (entity.size < DEFAULT_PAGE_SIZE) {
                        mAdapter.loadMoreEnd()
                    }
                }

                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    e.printStackTrace()
                    if (mRefreshLayout.isRefreshing()) {
                        mRefreshLayout.finishRefresh();
                    } else {
                        if (mAdapter.getData().size == 0) {
                            mEasyStatusView.error();
                        }
                    }
                }
            })


    }

    override fun onItemClicked(
        adapter: BaseQuickAdapter<AroundWaterEntity.DataBeanXX.DataBeanX, BaseViewHolder>?,
        view: View?,
        position: Int
    ) {

    }

    override fun getAdapter(): BaseQuickAdapter<AroundWaterEntity.DataBeanXX.DataBeanX, BaseViewHolder> =
        AroundWaterAdapter(null).apply {
            mAdapter = this
        }

}
